/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package giormi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author Tim
 */
public class viewOrders {
    private String SQLStatement;
    private Connection conn;
    private String URL;
    private Statement stmt;
    public ResultSet rset;
    protected JTable main;
    
    public viewOrders(JTable orders){
        this.main = orders;
    }
    
    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    public void viewOrders()
    {

        int i=0,nCtr=0;
            connect();
            
            SQLStatement = SQLStatement = "SELECT orderr.idorder, orderr.o_dateposted, "
                    + "customer.c_name, product.p_name,"
                    + "product.p_packing, stock.s_quedan, orderr.o_qtysold, orderr.o_qtyreturned,"
                    + "orderr.o_datedelivered, orderr.o_drdate, orderr.o_drnum, orderr.o_wsnum, "
                    + "orderr.o_wrnum, orderr.o_notes "
                    + "FROM orderr,customer,order_stock,stock,product "
                    + "WHERE orderr.o_idcustomer = customer.idcustomer AND orderr.o_order_stock_id = order_stock.idorder_stock "
                    + "AND order_stock.os_idstock = stock.idstock "
                    + "AND stock.s_idproduct = product.idproduct "
                    + "ORDER BY idorder";
            try {
            rset = stmt.executeQuery(SQLStatement);
            while(rset.next())
                nCtr++;
            rset.close();
            rset = stmt.executeQuery(SQLStatement);
            
            while(rset.next()){
                main.setValueAt(rset.getInt("orderr.idorder"),i,0);
                main.setValueAt(rset.getString("orderr.o_dateposted"),i,1);
                main.setValueAt(rset.getString("orderr.o_dateposted"),i,1);
                main.setValueAt(rset.getString("customer.c_name"),i,2);
                main.setValueAt(rset.getString("product.p_name"),i,3);
                main.setValueAt(rset.getString("product.p_packing"),i,4);
                main.setValueAt(rset.getString("stock.s_quedan"),i,5);
                main.setValueAt(rset.getInt("orderr.o_qtysold"),i,6);
                main.setValueAt(rset.getInt("orderr.o_qtyreturned"),i,7);
                main.setValueAt(rset.getString("orderr.o_qtysold"),i,6);
                main.setValueAt(rset.getString("orderr.o_qtyreturned"),i,7);
                main.setValueAt(rset.getString("orderr.o_datedelivered"),i,8);
                main.setValueAt(rset.getString("orderr.o_drdate"),i,9);
                main.setValueAt(rset.getString("orderr.o_drnum"),i,10);
                main.setValueAt(rset.getString("orderr.o_wsnum"),i,11);
                main.setValueAt(rset.getString("orderr.o_wrnum"),i,12);
                main.setValueAt(rset.getString("orderr.o_notes"),i,13);

                i++;
            }
            rset.close();
            
          disconnect();
            }
             catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
}
